const puppeteer = require('puppeteer');
let xlsx = require('node-xlsx');
let fs = require('fs');
let sheets = xlsx.parse('./new.xls');

let arr = [];
sheets.forEach(function (sheet) {
    var newSheetsArr = [];
    for (var i = 4; i < sheet["data"].length; i++) {
        var row = sheet['data'][i];
        if (row && row.length > 0) {
            let linkData = row[5];
            let count = row[2];
            let linkStr = ''
            if(linkData){
              linkStr = linkData.substring(linkData.lastIndexOf('http'))
            }
            newSheetsArr.push({
                class:row[1],
                title: row[3],
                link: row[5],
                count: row[2],
                linkStr: linkStr
            });
        }
    }
    arr.push(newSheetsArr);
    // fs.writeFile('./list.txt',JSON.stringify(arr),'utf8',(err)=>{})
});

(async () => {
  const link = {
    linkArr: arr[0]
  }
  const browser = await puppeteer.launch({
    defaultViewport :{width:1920,height:1080}});
 
  const linkArr = link.linkArr
  let i = 0
  async function forGet() {
    let  x = linkArr[i]
    const page = await browser.newPage();
    if(x.linkStr){
      await page.goto(x.linkStr, { waitUntil: ['domcontentloaded', 'load','networkidle0'] }).catch(err => {
        console.log('goto-err-> ' + err)
        fs.appendFile("./error.txt",`${x.class}\r\n ${x.title}\r\n${x.linkStr}\r\n========\r\n` , (error)  => {});
      })
    if(!fs.existsSync(`./${x.class}`)){
        fs.mkdirSync(x.class)
    }
    // await page.waitForSelector('img',{visible:true}).catch(err => {
    //     console.log('waitForSelector-err-> ' + err)
    //   })
    await page.screenshot({path: `./${x.class}/${x.title}.png`, fullPage: true}).catch(err => {
        console.log('screenshot-err-> ' + err)
        fs.appendFile("./error.txt",`${x.class}\r\n ${x.title}\r\n${x.linkStr}\r\n========\r\n` , (error)  => {});
      })
      fs.appendFile("./list.txt",`${x.class}\r\n ${x.title}\r\n${x.linkStr}\r\n========\r\n` , (error)  => {});
    i++
    if (i < linkArr.length) {
      forGet()
      console.log('loading.....'+(i+1)+'/'+arr[0].length)
    } else {
      await browser.close();
    }
    }
  }
 
  forGet()
})()